package com.community.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.dto.MerchantDto;
import com.community.dto.MerchantOrders;
import com.community.mapper.MerchantMapper;
import com.community.pojo.Merchant;
import com.community.service.IMerchantService;
import com.community.utils.PageUtils;
import com.community.vo.MerchantVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TangHaoSong Fly ChenJie HeYuHang
 * @since 2024-05-15
 */
@Service
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant>
        implements IMerchantService {

    @Autowired
    private MerchantMapper merchantMapper;


    @Override
    public boolean updateByDtoId(MerchantDto dto) {
        try {
            merchantMapper.updateMerchant(dto);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public void delByMerchantId(Merchant merchant) {
        if (merchant != null && merchant.getMerchantId() != null) {
            // 构建更新的值和条件
            UpdateWrapper<Merchant> updateWrapper = new UpdateWrapper<>();

            // 设置要更新的列和新值
            updateWrapper.set("is_deleted", 1)
                    .eq("merchant_id", merchant.getMerchantId())
                    .eq("is_deleted", 0);
            // 确保仅更新未删除的记录

            // 执行更新操作
            this.update(merchant, updateWrapper);
        }
    }

    @Override
    public PageInfo<Merchant> getMerchantList(int pageNo, int pageSize, Merchant merchant) {
        QueryWrapper<Merchant> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted", 0);

        // 添加额外的查询条件
        if (merchant.getName() != null) {
            wrapper.eq("name", merchant.getName());
        }
        if (merchant.getShopName() != null) {
            wrapper.eq("shop_name", merchant.getShopName());
        }

        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        // 查询语句
        List<Merchant> merchantInfos = merchantMapper.queryAll(merchant);
        // 封装分页结果
        return new PageInfo<>(merchantInfos);
    }

    @Override
    public PageUtils<Merchant> merchantFuHePage(Merchant merchant, int pageNo, int pageSize) {
        QueryWrapper<Merchant> wrapper=new QueryWrapper<Merchant>();
        //额外加2个条件
        //审核状态: 待审核
        wrapper.eq("check_Tag","S001-1");

        PageHelper.startPage(pageNo,pageSize);
        List<Merchant> merchantInfos=this.list(wrapper);
        PageInfo<Merchant> pageInfo=new PageInfo<Merchant>(merchantInfos);

        //把PageHelper PageUtils
        PageUtils<Merchant> pageUtils=new PageUtils<Merchant>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }

    @Override
    public List<MerchantOrders> merchantByConsumerId(Integer id) {
        return merchantMapper.merchantByConsumerId(id);
    }


}
